package echat.future.cn.echat.my.view;

import android.view.View;

import java.util.concurrent.TimeUnit;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityForgetBinding;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.PhoneUtils;
import echat.future.cn.echat.my.utils.ToastUtils;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import lumingweihua.future.cn.baselibgxh.utils.MD5;

public class ForgetActivity extends BaseActivity<ActivityForgetBinding> implements View.OnClickListener{

    @Override
    public int layoutId() {
        return R.layout.activity_forget;
    }

    @Override
    public IBaseNavagation navagation() {
        return new AdapterNavagation(this).setTitle(this.getResources().getString(R.string.go_forget_pass),16,R.color.color_333333).setBack();
    }

    @Override
    public void initView() {
        bind.next.setOnClickListener(this);
        bind.confirm.setOnClickListener(this);
        bind.tvCode.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.next:
                next();
                break;
            case R.id.confirm:
                restPass();
                break;
            case R.id.tvCode:
                getCode();
                break;
        }
    }

    /**
     * 下一步，校验手机号码和短信验证码
     */
    private void next(){
        //1校验手机号码
        if(!PhoneUtils.isMobile0(bind.phone.getText().toString())){
            toast("请输入正确的手机号码");
            return;
        }
        new HttpUtilsRequst<>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).checkForgetPass(bind.phone.getText().toString(),bind.code.getText().toString()))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<String>) o -> {
                    bind.step1.setVisibility(View.GONE);
                    bind.step2.setVisibility(View.VISIBLE);
                }).start();
    }


    /**
     * 获取手机号码
     */
    private void getCode(){
        //1校验手机号码
        if(!PhoneUtils.isMobile0(bind.phone.getText().toString())){
            toast("请输入正确的手机号码");
            return;
        }
        //禁止点击
        bind.tvCode.setClickable(false);
        Observable.interval(1, TimeUnit.SECONDS)
                .take(59)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        bind.tvCode.setClickable(false);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        bind.tvCode.setText("剩余" + (60 - aLong) + "秒");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        bind.tvCode.setClickable(true);
                        bind.tvCode.setText("获取验证码");
                    }
                });
        new HttpUtilsRequst<String>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).sendCode(bind.phone.getText().toString())).
                setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<String>) o -> {
                    toast("短信验证码已经发送到"+bind.phone.getText().toString());
                }).start();
    }


    /**
     * 重置手机号码
     */
    private void restPass(){
        if(!PhoneUtils.isMobile0(bind.phone.getText().toString())){
            ToastUtils.toastMsg(ForgetActivity.this,"请输入正确的手机号码");
            return;
        }
        if(!PhoneUtils.isPass(bind.pass.getText().toString())){
            ToastUtils.toastMsg(ForgetActivity.this,"请输入正确的密码");
            return;
        }
        String md5Pass = MD5.Md5(bind.pass.getText().toString()).toUpperCase();
        Observable observable =
                HttpUtilsRequst.retrofitClient.create(UserApi.class).restPsw(bind.phone.getText().toString(),md5Pass);
        new HttpUtilsRequst<String>(this,observable).
                setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<String>) o -> {
                    toast("充值密码成功");
                    finish();
                }).start();
    }

}
